package com.ilock.task;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ilock.bean.Ilockloc;
import com.ilock.bean.Ilocklog;
import com.ilock.service.ILockLocService;
import com.ilock.service.ILockLogService;
import com.ilock.service.ILockService;
import com.ilock.service.ILockStateService;

public class TimeCheckTask {
	private ILockLogService ILockLogService;
	private ILockService ILockService;
	private ILockLocService ILockLocService;
	private ILockStateService ILockStateService;
	
	public ILockLogService getILockLogService() {
		return ILockLogService;
	}

	public void setILockLogService(ILockLogService iLockLogService) {
		ILockLogService = iLockLogService;
	}

	public ILockService getILockService() {
		return ILockService;
	}

	public void setILockService(ILockService iLockService) {
		ILockService = iLockService;
	}
	
	public ILockLocService getILockLocService() {
		return ILockLocService;
	}

	public void setILockLocService(ILockLocService iLockLocService) {
		ILockLocService = iLockLocService;
	}

	public ILockStateService getILockStateService() {
		return ILockStateService;
	}

	public void setILockStateService(ILockStateService iLockStateService) {
		ILockStateService = iLockStateService;
	}

	public void LockCheckTask(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(new Date())+" lock connection check");
		List loseConnLockList = ILockStateService.getLostConnLockList();
		System.out.println(loseConnLockList);
		if(loseConnLockList!=null){
			for(int i=0;i<loseConnLockList.size();i++){
				int templogNumber = (Integer) loseConnLockList.get(i);
				System.out.println("find lose connection lock:"+templogNumber);
				Ilockloc temploseloc = ILockLocService.getLockLatestLocation(templogNumber);
				String alertContent = "alert_lat"+temploseloc.getIlockLocLat()+"_lng"+temploseloc.getIlockLocLng()+"_state_loseconn";
				List nearLockList = ILockLocService.getNearLockList(templogNumber, temploseloc.getIlockLocLng(), temploseloc.getIlockLocLat(), 100);
				
				//alert self
				Ilocklog ownLog = new Ilocklog();
				ownLog.setIlockInfoNumber(templogNumber);
				ownLog.setPriority(4);
				ownLog.setSource(0);
				ownLog.setContent("stateloseconn");
				ownLog.setDate(Timestamp.valueOf(sdf.format(new Date())));
				ILockLogService.addLockLog(ownLog);
				//alert near lock user
				for(int j=0;j<nearLockList.size();j++){
					Ilockloc tempnearloc =(Ilockloc) nearLockList.get(j);
					Ilocklog loseLog = new Ilocklog();	
					loseLog.setIlockInfoNumber(tempnearloc.getIlockInfoNumber());
					loseLog.setPriority(4);
					loseLog.setSource(0);
					loseLog.setContent(alertContent);
					loseLog.setDate(Timestamp.valueOf(sdf.format(new Date())));
					ILockLogService.addLockLog(loseLog);
					System.out.println("alert near user with lockID:"+tempnearloc.getIlockInfoNumber());
				}
			}
		}
	}

}
